<?php

class HTech_Model_Bookmark extends Zend_Db_Table_Abstract
{
    public function getBookmarks($userId = null)
    {    
        $sql = sprintf("
            select
                concat(r.Module,'.',r.Controller,'.',r.Action) resource,
                b.Bookmark
            from
                bookmarks b, resources r, lnk_resources_roles rr
            where
                b.UserId = %s and r.ResourceId = b.ResourceId and rr.RoleId = %s and rr.ResourceID = r.ResourceId", 
                self::_getUserId($userId), self::_getRoleId());       
        return $this->getAdapter()->fetchPairs($sql);
    }

    public function getJsonItems($userId = null)
    {   
        $bookmarks = self::getBookmarks($userId);
        $c = 0;
        $items = array();
        foreach($bookmarks as $resource => $bookmark) {
            $items[$c]['id'] = $resource;
            $items[$c]['name'] = $bookmark; 
            $items[$c]['url'] = '/'. str_replace('.','/',$resource);
            $c++;
        }
        return $items;
    }

    protected function _getUserId($userId = null)
    {
        return $userId ? $userId : Zend_Auth::getInstance()->getIdentity()->UserId;
    }
    
    protected function _getRoleId($roleId = null)
    {
        return $roleId ? $roleId : Zend_Auth::getInstance()->getIdentity()->RoleId;
    }
    
}